<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>意向客户</title>
    <link rel="stylesheet" href="${ctxPath}/assets/libs/layui/css/layui.css"/>
    <link rel="stylesheet" href="${ctxPath}/assets/module/admin.css"/>
    <link rel="stylesheet" href="${ctxPath}/assets/libs/zTree/css/zTreeStyle/zTreeStyle.css"/>
</head>

<body>
<% include("../layout/loading.html"){} %>

<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <div class="layui-form toolbar">
                <form>
                    <div class="layui-form-item">
                        <label class="layui-form-label">提交时间:</label>
                        <div class="layui-input-inline" style="width: 230px">
                            <input type="text" class="layui-input date-icon" id="searchDate" placeholder="请选择时间"
                                   name="searchDate" autocomplete="off" >
                        </div>
                        <label class="layui-form-label">所在地区:</label>
                        <div class="layui-input-inline" style="width: 200px">
                            <select id="provinceId" name="provinceId" xm-select="provinceId" lay-verType="tips"
                                    lay-filter="schoolWebsiteType">
                                <option value="">请选择省</option>
                                <% for(province in provinceList) { %>
                                <option value="${province.id}_${province.code}">${province.name}</option>
                                <% } %>
                            </select>
                        </div>
                        <div class="layui-input-inline" style="width: 200px">
                            <select id="cityId" name="cityId" id="cityId" xm-select="cityId" lay-verType="tips"
                                    lay-filter="schoolWebsiteCityType">
                                <option value="">请选择市:</option>

                            </select>
                        </div>
                        <div class="layui-input-inline" style="width: 200px">
                            <select id="areaId" name="areaId" id="areaId">
                                <option value="">请选择县/区:</option>

                            </select>
                        </div>
                        <div class="layui-inline">
                            <button lay-submit="" lay-filter="btnSearchSubmit" type="submit" class="layui-btn icon-btn">
                                <i class="layui-icon">&#xe615;</i>查询
                            </button>
                            <button id="btnReset" class="layui-btn icon-btn"><i class="layui-icon">&#xe669;</i>重置
                            </button>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <button id="btnExport" type="button" lay-filter="btnExport" class="layui-btn icon-btn"><i class="layui-icon">&#xe67d;</i>导出</button>
                    </div>
                </form>
            </div>

            <table class="layui-table" id="linkTable" lay-filter="linkTable"></table>
        </div>
    </div>
</div>


<!-- js部分 -->
<script type="text/javascript" src="${ctxPath}/assets/libs/jquery/jquery-3.2.1.min.js"></script>
<script type="text/javascript" src="${ctxPath}/assets/libs/zTree/js/jquery.ztree.all-3.5.min.js"></script>
<script type="text/javascript" src="${ctxPath}/assets/libs/layui/layui.js"></script>
<script type="text/javascript" src="${ctxPath}/assets/js/common.js"></script>
<script>
    layui.use(['layer', 'form', 'table', 'util', 'admin', 'laydate','excel'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var laydate = layui.laydate;
        var util = layui.util;
        var admin = layui.admin;
        window.excel = layui.excel;
        laydate.render({elem: '#searchDate', range: true, value: '', isInitValue: true});
        var cols = [
            {field: 'linkTel', title: '手机号'},
            {field: 'provinceNames', title: '所在地区'},
            {field: 'address', title: '具体地址' , width:200},
            {field: 'createdDate', title: '提交时间' , width:200}
        ];
        // 渲染表格
        var insTb = table.render({
            elem: '#linkTable',
            url: '/admin/link/list',
            page: true,
            cellMinWidth: 100,
            cols: [[
                // {type: 'numbers'},
                {field: 'linkTel', title: '手机号'},
                {field: 'provinceNames', title: '所在地区'},
                {field: 'address', title: '具体地址'},
                {field: 'createdDate', title: '提交时间'}
                // {align: 'center', toolbar: '#tableBar', title: '操作', minWidth: 200}
            ]],

            done: function (res) {
                exportData = res.data;
            }
        });

        //监听提交搜索
        form.on('submit(btnSearchSubmit)', function (data) {
            var res = data.field;
            insTb.reload({where: res, page: 1});
            return false;
        });

        // 重置
        $('#btnReset').click(function () {
            $('#edtSearch').val("");
            window.location.reload();
        });
        //联动,市
        form.on('select(schoolWebsiteType)', function (data) {
            var val = data.value;
            var  code=val.split("_");
            // console.log(code);
            var text = data.elem[data.elem.selectedIndex].text;
            // console.log(text);
            $.ajax({
                type: 'GET',
                url: '/admin/address/city/list',
                data: {code: code[1], type: 1},
                dataType: 'json',
                success: function (data) {
                    // console.log(data.data);
                    $("#cityId").empty();
                    $('#cityId').append(new Option("请选择", ""));// 下拉菜单里添加元素
                    $.each(data.data, function (index, item) {
                        $('#cityId').append(new Option(item.name, item.id+'_'+item.code));// 下拉菜单里添加元素
                    });
                    form.render("select");//重新渲染 固定写法
                }
            });
        });
        //联动,区
        form.on('select(schoolWebsiteCityType)', function (data) {
            var val = data.value;
            var  code=val.split("_");
            var text = data.elem[data.elem.selectedIndex].text;
            $.ajax({
                type: 'GET',
                url: '/admin/address/city/list',
                data: {code: code[1], type: 2},
                dataType: 'json',
                success: function (data) {
                    // console.log(data.data);
                    $("#areaId").empty();
                    $('#areaId').append(new Option("请选择", ""));// 下拉菜单里添加元素
                    $.each(data.data, function (index, item) {
                        $('#areaId').append(new Option(item.name, item.id+'_'+item.code));// 下拉菜单里添加元素
                    });
                    form.render("select");//重新渲染 固定写法
                }
            });
        });
        //导出
        $("#btnExport").click(function(){
            var provinceId = $('#provinceId').val();
            var cityId = $('#cityId').val();
            var areaId = $('#areaId').val();
            var searchDate = $('#searchDate').val();
            var startDate = searchDate.substring(0, 10);
            var endDate = searchDate.substring(13);
            var where = ()=> ({
                provinceId: provinceId,
                cityId: cityId,
                areaId: areaId,
                startDate: startDate,
                endDate: endDate + " 23:59:59"
            }) ;
            var data = where();
            data.page = 1;
            data.limit = 999999999;//不分页
            toGet("/admin/link/list",data,function(res){
                // console.log(res);
                var [t,f] = [{},[]];//标题/字段;
                var DATA = res.data;
                $(cols).each(function(k,v){
                    t[v.field]=v.title;
                    f.push(v.field);
                });
                // console.table(DATA);
                // 重点！！！如果后端给的数据顺序和映射关系不对，请执行梳理函数后导出
                DATA = excel.filterExportData(DATA,f);
                // 重点2！！！一般都需要加一个表头，表头的键名顺序需要与最终导出的数据一致
                DATA.unshift(t);
                excel.exportExcel(DATA, '意向客户列表'+(data.limitTime||"")+'.xlsx', 'xlsx');
                //console.log(col);
                // console.table(DATA);
            });
            function toGet(url,data,callback){
                top.layer.load(2);
                $.get(url, data , function (res) {
                    top.layer.closeAll('loading');
                    if(res.msg)layer.msg(res.msg, {icon: !res.code ? 1 :2});
                    if(!res.code&&callback)callback(res);
                }, 'json');
            }

        });
    });
</script>
</body>
</html>